package com.ddz.qpz.controller.admin;

import com.ddz.common.exception.BusinessException;
import com.ddz.common.result.BaseResult;
import com.ddz.common.result.ResponseEnum;
import com.ddz.qpz.pojo.entity.IntegralGrade;
import com.ddz.qpz.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author LiQingBo
 * @since 2021-10-04
 */
@Slf4j
@Api(tags = "积分等级管理")
//@CrossOrigin
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {
    @Resource
    private IntegralGradeService integralGradeService;

    @ApiOperation("积分等级列表")
    @GetMapping("/list")
    public BaseResult listAll() {
        List<IntegralGrade> list = integralGradeService.list();
        return BaseResult.ok().data("list", list).message("获取列表成功");
    }

    @ApiOperation(value = "根据id删除记录", notes = "逻辑删除记录")
    @DeleteMapping("/remove/{id}")
    public BaseResult removeById(@PathVariable Long id) {
        boolean b = integralGradeService.removeById(id);
        if (b) {
            return BaseResult.ok().message("删除成功");
        } else {
            return BaseResult.error().message("删除失败");
        }
    }

    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public BaseResult save(@ApiParam(value = "积分等级对象", required = true)
                           @RequestBody IntegralGrade integralGrade) {
        if (integralGrade.getBorrowAmount() == null) {
            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        }
        boolean save = integralGradeService.save(integralGrade);
        if (save) {
            return BaseResult.ok().message("保存成功");
        } else {
            return BaseResult.error().message("保存失败");
        }
    }

    @ApiOperation("根据id获取积分等级")
    @GetMapping("/get/{id}")
    public BaseResult getById(@ApiParam(value = "数据id", required = true, example = "1") @PathVariable Long id) {
        IntegralGrade byId = integralGradeService.getById(id);
        if (byId != null) {
            return BaseResult.ok().data("record", byId);
        } else {
            return BaseResult.error().message("数据获取失败");
        }
    }

    @ApiOperation("更新积分等级")
    @PutMapping("/update")
    public BaseResult updateById(@ApiParam(value = "积分等级对象", required = true)
                                 @RequestBody IntegralGrade integralGrade) {
        boolean update = integralGradeService.updateById(integralGrade);
        if (update) {
            return BaseResult.ok().message("更新成功");
        } else {
            return BaseResult.error().message("更新失败");
        }
    }
}

